//
//  HSLotteryNumberCell.m
//  HolyShit
//
//  Created by Robin on 2017/4/29.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSLotteryNumberCell.h"

@implementation HSLotteryNumberModel

- (void)setOpen_code:(NSString *)open_code
{
    _open_code = open_code;
    NSArray<NSString *> *tmpStr = [open_code componentsSeparatedByString:@"|"];
    if (tmpStr.count < 1) {
        return;
    }
    self.numbers = [tmpStr[0] componentsSeparatedByString:@","].mutableCopy;
    self.normalCount = self.numbers.count;
    if (tmpStr.count < 2) {
        return;
    }
    [self.numbers addObjectsFromArray:[tmpStr[1] componentsSeparatedByString:@","]];
}

@end

@interface HSLotteryNumberCell ()

@property (nonatomic, strong) UILabel *nameLb;
@property (nonatomic, strong) UILabel *timeLb;
@property (nonatomic, strong) UILabel *numLb;
@property (nonatomic, strong) UIView *shishiView;
@property (nonatomic, strong) NSMutableArray<UILabel *> *subLbs;

@end

@implementation HSLotteryNumberCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self commonInit];
    }
    return self;
}

static const CGFloat labelWid = 30;
static const CGFloat labelPad = 10;

- (void)commonInit
{
    self.subLbs = @[].mutableCopy;
    self.backgroundColor = WHITE_COLOR;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.width = ScreenWidth;
    self.nameLb = [UILabel labelWithFont:MYFont(Font32) color:HEXCOLOR(HSColorBlack)];
    [self addSubview:self.nameLb];
    self.timeLb = [UILabel labelWithFont:MYFont(Font26) color:HEXCOLOR(HSColorGray)];
    [self addSubview:self.timeLb];
    self.numLb = [UILabel labelWithFont:MYFont(Font26) color:HEXCOLOR(HSColorGray)];
    [self addSubview:self.numLb];
    UILabel *arrowLb = [UILabel labelWithFrame:CGRectZero font:IconFont(20) color:HEXCOLOR(HSColorGray) text:@""];
    [self addSubview:arrowLb];
    self.shishiView = [UIView new];
    self.shishiView.clipsToBounds = YES;
    [self addSubview:self.shishiView];
    
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
    }];
    [self.numLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLb);
        make.top.equalTo(self.nameLb.mas_bottom).offset(5);
    }];
    
    UIView *line = [UIView new];
    line.backgroundColor = HEXCOLOR(HSColorSeparateLight);
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.numLb);
        make.left.equalTo(self.numLb.mas_right).offset(5);
        make.width.mas_equalTo(1);
    }];
    
    [self.timeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line.mas_right).offset(5);
        make.centerY.equalTo(self.numLb.mas_centerY);
    }];
    
    [self.shishiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLb);
        make.top.equalTo(self.numLb.mas_bottom).offset(10);
        make.height.mas_equalTo(labelWid);
    }];
    
    [arrowLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.equalTo(self.shishiView);
    }];
    [self showLineForDirection:MYDirectionBottom leftPad:10];
}

- (UILabel *)shiLabelWithFrame:(CGRect)frame title:(NSString *)title
{
    UILabel *lb = [UILabel labelWithFrame:frame font:MYFont(Font30) color:WHITE_COLOR text:title];
    lb.textAlignment = NSTextAlignmentCenter;
    lb.backgroundColor = [UIColor redColor];
    [lb makeRound:YES];
    return lb;
}

- (void)setModel:(HSLotteryNumberModel *)model
{
    _model = model;
    self.nameLb.text = model.lot_name;
    self.numLb.text = [NSString stringWithFormat:@"第%@期",model.expect];
    self.timeLb.text = [NSString stringWithFormat:@"开奖时间%@", model.opencode_time];
    
    NSUInteger count = model.numbers.count;
    for (NSUInteger idx = 0 ; idx < count; ++idx) {
        UILabel *lb;
        if (idx >= self.subLbs.count) {
            lb = [self shiLabelWithFrame:CGRectMake( idx * (labelWid + labelPad), 0, labelWid, labelWid) title:model.numbers[idx]];
            [self.shishiView addSubview:lb];
            [self.subLbs addObject:lb];
        }
        else{
            lb = self.subLbs[idx];
            lb.text = model.numbers[idx];
        }
        lb.backgroundColor = idx < model.normalCount ? HEXCOLOR(HSColorRed) : HEXCOLOR(HSColorBlue);
    }
    [self.shishiView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(model.numbers.count * (labelWid + labelPad));
    }];
}

@end
